//
//  THJaAdmitCompanyInfoVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/12/19.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJaAdmitCompanyInfoVC.h"

@interface THJaAdmitCompanyInfoVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSInteger imgIndex;
}
@property (weak, nonatomic) IBOutlet UITextField *tf1;
@property (weak, nonatomic) IBOutlet UITextField *tf2;
@property (weak, nonatomic) IBOutlet UIImageView *imgV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@end

@implementation THJaAdmitCompanyInfoVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(admitInfo)];
    self.imgV.userInteractionEnabled = YES;
    [self.imgV addGestureRecognizer:tap];
    if(KIsiPhoneX){
        self.topSpace.constant = 84;
        [self setXNavBarWithTitle:@"企业认证"];
    }else{
        [self setNavBarWithTitle:@"企业认证"];
    }
}
-(void)admitInfo{
    [self alertWithTitle1:@"取消" title2:@"拍照" title3:@"相册"];
}
#pragma mark ----更多操作---
-(void)actionSheetIndex0Function{
    NSLog(@"取消1");
}
-(void)actionSheetIndex1Function{
    [self getPhotoWithType:0];
}
-(void)actionSheetIndex2Function{
    [self getPhotoWithType:1];
}

/**
 调用相册或相机
 */
-(void)getPhotoWithType:(NSInteger)index{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (index) {
            case 0:
                //来源:相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                //来源:相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.navigationBar.translucent = NO;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.imgV.image = image;
    imgIndex = 99;
}
- (IBAction)sureBtnAction:(id)sender {
    if(!self.tf1.text.length){
        [self HUDShowMsg:@"请填写企业名称" AfterDelayDimiss:1];
        return;
    }else if (!self.tf2.text.length){
        [self HUDShowMsg:@"请填写营业执照号" AfterDelayDimiss:1];
        return;
    }else if(imgIndex != 99){
        [self HUDShowMsg:@"请选择营业执照照片" AfterDelayDimiss:1];
        return;
    }
    NSString * token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    NSString * imgData = [self image2DataURL:self.imgV.image];
    NSDictionary * dic = @{@"company":self.tf1.text,
                           @"licence_code":self.tf2.text,
                           @"company_licence":imgData
                           };
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=member&r=member&m=open_company"];
    [THJNetWork postDataFromUrl:url parameters:dic Token:token callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"开通商铺-%@",receiveData);
        [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if ([receiveData[@"status"] isEqual:@"200"]){
            [self performSelector:@selector(pop) withObject:nil afterDelay:1];
        }
    }];
}
@end
